package com.db.example;

import java.io.IOException;

import com.db.mylogging.AsyncAppender;
import com.db.mylogging.FileAppender;
import com.db.mylogging.MyLogger;

/**
 * This class demonstrates direct usage (as opposed to usage via SLF4J) of
 * MyLogger class.
 */
public class LoggingDemo {
	public static void main(String[] args) throws IOException {
		MyLogger logger;

		System.out.println("Using FileAppender");
		logger = new MyLogger(LoggingDemo.class.getName());
		logger.addAppender(new FileAppender("log.txt", false));
		benchmark(logger);
		logger.close();

		System.out.println("Using AsyncAppender");
		logger = new MyLogger(LoggingDemo.class.getName());
		logger.addAppender(new AsyncAppender(new FileAppender("log_async.txt", false)));
		benchmark(logger);
		logger.close();
		System.out.println("AsyncAppender finished work");
	}

	private static void benchmark(MyLogger logger) {
		long start = System.nanoTime();
		for (int i = 0; i < 100_000; i++) {
			logger.info("iteration number: " + i);
		}
		long time = System.nanoTime() - start;
		System.out.printf("%,d ns%n", time);
	}
}
